// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString, spellCheckIdentifiers, spellCheckStringLiterals, logExceptions
/**
 * Package db
 */
package teamrocket.db; // $codepro.audit.disable packagePrefixNamingConvention

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;


import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
 * Interface class to interact with the database
 * @author Stephen
 *
 * @version $Revision: 1.0 $
 */
public class DBWrapper {
	
	/**
	 * Field DEBUG.
	 */
	public static boolean DEBUG = false;
	
	/**
	 * Field con.
	 */
	static Connection Con; // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.avoidPackageScopeAuditRule
	
	static
	{
		try {
			Class.forName("com.mysql.jdbc.Driver"); // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.unusedReturnValue
			Con = (Connection) DriverManager.getConnection(
					"jdbc:mysql://mysql.alphastrikeproductions.com/alphastr_academic",
					"academic_mysql",
					"cs2340group7");
		} catch (ClassNotFoundException e) {
			System.out.println("Error finding drivers for connection.");
		} catch (SQLException e) {
			System.out.println("Error connecting to DB.");
		}
	}

	/**
	 * Executes a query on the database that does not expect results
	 * @param sql
	 * @param params
	 * @return boolean
	 * @throws SQLException
	 */
	public static boolean execute(String sql, Object ... params) throws SQLException { // $codepro.audit.disable booleanMethodNamingConvention
		final PreparedStatement cmd = (PreparedStatement) Con.prepareStatement(sql);
		int index=1;
		for(Object o : params) {
			cmd.setString(index, o.toString());
			index++;
		}

		return cmd.execute();
	}

	/**
	 * Executes a query on the db that expects a set of results back
	 * @param sql
	 * @param params
	 * @return ResultSet
	 * @throws SQLException
	 */
	public static ResultSet query(String sql, Object ... params) throws SQLException {
		final PreparedStatement cmd = (PreparedStatement) Con.prepareStatement(sql);
		int index=1;
		for(Object o : params) {
			cmd.setString(index, o.toString());
			index++;
		}

		return cmd.executeQuery();
	}

	/**
	 * Closes the connection to the database
	 */
	public static void close() {
		try {
			Con.close(); // $codepro.audit.disable closeInFinally
		} catch (SQLException e) {
			System.out.println("Error closing connection to mysql database.");
		}
	}

}
